﻿@*
    Copyright 2014 Francois Karman

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*@
@model Quartz.WebManager.Web.Models.WebTrigger
<h1>Define the trigger</h1>
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                @Html.LabelFor(m => m.Name)
                @Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
                <span class="help-block">The name of this trigger.</span>
                @Html.ValidationMessageFor(m => m.Name)
            </div>
            <div class="form-group">
                @Html.LabelFor(m => m.Group)
                @Html.TextBoxFor(m => m.Group, new { @class = "form-control" })
                <span class="help-block">The group of the trigger.</span>
                @Html.ValidationMessageFor(m => m.Group)
            </div>
        </div>
        <div class="form-group col-md-6">
            @Html.LabelFor(m => m.Description)
            @Html.TextAreaFor(m => m.Description, 8, 0, new { @class = "form-control" })
            @Html.ValidationMessageFor(m => m.Description)
        </div>
    </div>
    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                @Html.LabelFor(m => m.StartTimeUtc)
                @Html.TextBoxFor(m => m.StartTimeUtc, new { @class = "form-control datetime" })
                <span class="help-block">The starting date of this trigger, if any.</span>
                @Html.ValidationMessageFor(m => m.StartTimeUtc)
            </div>
            <div class="form-group">
                @Html.LabelFor(m => m.EndTimeUtc)
                @Html.TextBoxFor(m => m.EndTimeUtc, new { @class = "form-control datetime" })
                <span class="help-block">The ending date of this trigger, if any.</span>
                @Html.ValidationMessageFor(m => m.EndTimeUtc)
            </div>
            <div class="form-group">
                @Html.LabelFor(m => m.Priority)
                @Html.TextBoxFor(m => m.Priority, new { @class = "form-control" })
                <span class="help-block">The priority of this trigger, if any.</span>
                @Html.ValidationMessageFor(m => m.Priority)
            </div>
        </div>
        <div class="col-md-6">
            <div class="form-group">
                @Html.LabelFor(m => m.MisfireInstruction)
                @Html.DropDownListFor(m => m.MisfireInstruction, Model.MisfireInstructions.Select(p => new SelectListItem { Text = p.Value, Value = p.Key.ToString(), Selected = p.Key == Model.MisfireInstruction }), new { @class = "form-control" })
                <span class="help-block">Instruction to manage misfire of the trigger.</span>
                @Html.ValidationMessageFor(m => m.MisfireInstruction)
            </div>
            <div class="form-group">
                @Html.LabelFor(m => m.CronExpression)
                @Html.TextBoxFor(m => m.CronExpression, new { @class = "form-control" })
                <span class="help-block">The cron expression used by this trigger.</span>
                @Html.ValidationMessageFor(m => m.CronExpression)
            </div>
        </div>
    </div>
    @Html.HiddenFor(m => m.JobGroup)
    @Html.HiddenFor(m => m.JobName)
    <button type="submit" class="btn btn-primary">Create</button>
    <a href="@Url.Action("AddJobStep1")" class="btn btn-link">Cancel</a>
}
@section styles
{
    <style type="text/css">
        .body-content form {
            margin-top: 40px;
        }

        .input-group-addon {
            cursor: default;
        }
    </style>
}
@section scripts
{
    <script>
        function addRequired(searchClass) {
            $(searchClass).wrap("<div class='input-group'>");
            $(searchClass).after("<span class='input-group-addon required'><span class='glyphicon glyphicon-asterisk'></span></span>");
        }

        $(function () {
            addRequired('input[type=text][data-val-required]');
        })

        $(function () {
            var nowTemp = new Date();
            var now = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), nowTemp.getDate(), 0, 0, 0, 0);

            $('.datetime')
                .attr("data-date-format", "DD MMM YYYY hh:mm:ss")
                .datetimepicker({ minDate: now })
                .on("dp.error", function (e) {
                    if (e.currentTarget.value !== "") {
                        // Format error
                    }
                });

            $("#StartTimeUtc").on("dp.change", function (e) {
                $('#EndTimeUtc').data("DateTimePicker").setMinDate(e.date);
            })
            $("#StartTimeUtc").on("dp.error", function (e) {
                $('#EndTimeUtc').data("DateTimePicker").setMinDate(now);
            });
            $("#EndTimeUtc").on("dp.change", function (e) {
                $('#StartTimeUtc').data("DateTimePicker").setMaxDate(e.date);
            });
        });
    </script>
}